<?php

namespace app\admin\controller\sys;
use app\admin\controller\Base;
use think\facade\Request;
use think\facade\Db;
use think\facade\View;

class System extends Base {
    
    public function initialize()
    {
        parent::initialize(); 
    }

    public function index(){
        if (Request::isPost()) {
            $data = $this->request->post();
            try{
                $info = Db::name('config')->select();
                foreach($info as $k=>$v){
                    $keyArr[] = $k;
                }
                foreach ($data as $key => $value) {
                    $currentData = array();
                    $currentData['value'] = $value;
                    if(in_array($key,$keyArr)){
                        Db::name('config')->where('name',$key)->update($currentData);
                    }else{
                        $this->error('配置项【'.$key.'】不存在，请现在数据库配置');
                    }
                }
            }catch(\Exception $e){
                exit($e->getMessage());
            }
            $this->success('保存成功',url('index'));
        }
        $extraConf = config()['global_system'];
        $confGroup = $extraConf['configgroup'];
        $list = Db::name('config')->select();

        View::assign(['system'=>$list,'confGroup'=>$confGroup]);
        return view();

    }
//    配置列表
    public function configlist(){

        if (Request::isPost()) {
            $keys = $this->request->post('keys', '', 'trim');
            $page = $this->request->post('page') ? $this->request->post('page') : 1;
            $limit = $this->request->post('limit');
            $list = Db::name('config')
                ->where('name', 'like', '%' . $keys . '%')
                ->paginate(['list_rows' => $limit, 'page' => $page])
                ->toArray();
            return $result = ['code' => 0, 'msg' => lang('get info success'), 'data' => $list['data'], 'count' => $list['total']];
        }

        return view();

    }
    //添加配置
    public function configAdd(){
        if(Request::isPost()){
            $data = $this->request->post();
            $info = Db::name('config')->select();
            foreach($info as $k=>$v){
                $keyArr[] = $v['name'];
            }
            if(in_array($data['name'],$keyArr)){
                $this->error('字典已存在','dict');
            }else{
                Db::name('config')->insert($data);
            }
            $this->success(lang('add success'),url('configlist'));
        }else{
            $extraConf = config()['global_system'];
            $confGroup = $extraConf['configgroup'];
            $view = ['info'=>null,'confGroup'=>$confGroup];
            View::assign($view);
            return view('add');
        }
        
        
    }
//    编辑配置
    public function configEdit(){
        $id = Request::param('id');
        if(Request::isPost()){
            $data = $this->request->post();
            Db::name('config')->where('id',$data['id'])->update($data);
            $this->success(lang('edit success'),url('configlist'));
        }
        $extraConf = config()['global_system'];
        $confGroup = $extraConf['configgroup'];
        $info = Db::name('config')->find($id);

        $view = ['info'=>$info,'confGroup'=>$confGroup];
        View::assign($view);
        return view('add');
    }
//    删除配置
    public function configDel(){
        if (Request::isPost()) {
            $ids = $this->request->post('ids');
            Db::name('config')->where('id','in',$ids)->delete();
            $this->success(lang('delete success'));
        } else {
            $this->error(lang('invalid options'));
        }

    }

}